﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Xml.Serialization;

namespace CompanyDic.SearchEngine
{
    public class UrlBuilder
    {
        private static CityHelper CityHelper;
        public static Dictionary<string, int> CityCurPageNoDic = new Dictionary<string, int>();

        public UrlBuilder()
        {
            using (Stream stream = Assembly.GetEntryAssembly().GetManifestResourceStream(Constants.CitiesFile))
            {
                XmlSerializer xs = new XmlSerializer(typeof(CityHelper));
                CityHelper = xs.Deserialize(stream) as CityHelper;
            }

            if (CityHelper == null)
                return;

            foreach (City c in CityHelper.Cities)
            {
                CityCurPageNoDic.Add(c.Name, 1);
            }
        }

        public Dictionary<string, string> GetSummaryUrls()
        {
            string urlFormat = "http://sou.zhaopin.com/jobs/jobsearch_jobtype.aspx?jl={0}&sm=0&p={1}";
            Dictionary<string, string> urls = new Dictionary<string, string>();

            string[] arr = new string[CityCurPageNoDic.Count];
            CityCurPageNoDic.Keys.CopyTo(arr, 0);
            for (int i = 0; i < arr.Length; i++)
            {
                string sourceUrl = string.Format(urlFormat, HttpUtility.UrlEncode(arr[i]), CityCurPageNoDic[arr[i]]);
                urls.Add(sourceUrl, arr[i]);
                CityCurPageNoDic[arr[i]]++;
            }

            return urls;
        }

        public void RemoveCity(string city)
        {
            if (CityCurPageNoDic.ContainsKey(city))
                CityCurPageNoDic.Remove(city);
        }
    }
}
